<!DOCTYPE html>
<HTML>
<HEAD>
	<TITLE></TITLE>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" href="/asset/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
	<script type="text/javascript" src="/asset/zTree/js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="/asset/zTree/js/jquery.ztree.core.js"></script>
	<script type="text/javascript" src="/asset/zTree/js/jquery.ztree.excheck.js"></script>
	<script type="text/javascript" src="/asset/zTree/js/jquery.ztree.exedit.js"></script>
	

	<SCRIPT type="text/javascript">
		<!--
		var setting = {
			
			edit: {
				enable: true,
				showRemoveBtn: false,
				showRenameBtn: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onRightClick: OnRightClick,
				beforeRename: beforeRename,
				beforeDrop: beforeDrop,
				onClick: zTreeOnClick
			}
		};

		var zNodes =[
			{id:0, pId:-1, name:"全部部门", open:true,icon:"/asset/zTree/css/zTreeStyle/img/diy/1_open.png"}
			<#list orgs as item>
			,{id:${item.organizationId}, pId:${item.parentId}, name:"${item.name}", open:true}
			</#list>
  	 	];

		function OnRightClick(event, treeId, treeNode) {
			if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
				zTree.cancelSelectedNode();
				showRMenu("root", event.clientX, event.clientY);
			} else if (treeNode && !treeNode.noR) {
				zTree.selectNode(treeNode);
				showRMenu("node", event.clientX, event.clientY);
			}
		}

		function showRMenu(type, x, y) {
			$("#rMenu ul").show();
			if (type=="root") {
				$("#m_del").hide();
			} else {
				$("#m_del").show();
			}

            y += document.body.scrollTop;
            x += document.body.scrollLeft;
            rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});

			$("body").bind("mousedown", onBodyMouseDown);
		}
		function hideRMenu() {
			if (rMenu) rMenu.css({"visibility": "hidden"});
			$("body").unbind("mousedown", onBodyMouseDown);
		}
		function onBodyMouseDown(event){
			if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
				rMenu.css({"visibility" : "hidden"});
			}
		}
		var addCount = 1;
		function addTreeNode() {
			hideRMenu();
			var newNode = { name:"增加" + (addCount++)};
			if (zTree.getSelectedNodes()[0]) {
				newNode.checked = zTree.getSelectedNodes()[0].checked;
				zTree.addNodes(zTree.getSelectedNodes()[0], newNode);
			} else {
				zTree.addNodes(null, newNode);
			}
		}
		function removeTreeNode() {
			hideRMenu();
			var nodes = zTree.getSelectedNodes();
			if (nodes && nodes.length>0) {
				if (nodes[0].children && nodes[0].children.length > 0) {
					var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
					if (confirm(msg)==true){
						zTree.removeNode(nodes[0]);
					}
				} else {
					zTree.removeNode(nodes[0]);
				}
			}
		}
		

		var zTree, rMenu;
		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting, zNodes);
			zTree = $.fn.zTree.getZTreeObj("treeDemo");
			rMenu = $("#rMenu");
		});
		
		
		
		//添加机构
		function addTreeNode() {
			hideRMenu();
			
			var field = {name:'新节点'};
			
			var treeNode = zTree.getSelectedNodes()[0];
			if(treeNode){
				field.parentId = treeNode.id;
			}else{
				field.parentId = 0;
			}
			
				
			$.ajax({
	  			type:"POST", 
	  			url:"/org/add.htm",
	  			data:field ,
	  			success:function(result) {
	  			
	  				var id = result.data.id;
	  				var pid = result.data.pid;
	  				var name = result.data.name;
		      		
		      		
		      		if(result.code=='1'){
		      			
		      			console.log(pid);
		      			
		      			if(treeNode){
			      			treeNode = zTree.addNodes(treeNode, {id:id, pId:pid, isParent:false, name:name});
			      		}else{
			      			var pnode = zTree.getNodeByParam("id", 0, null);
			      			treeNode = zTree.addNodes(pnode, {id:id, pId:pid, isParent:false, name:name});
			      		}
		      			
						if (treeNode) {
							zTree.editName(treeNode[0]);
						} else {
							alert("叶子节点被锁定，无法增加子节点");
						}
		      			
		      		}else{
		      		 	alert(result.msg);
		      		}
		      		
		    	 	} 
		    }); 
		};
		
		//删除机构
		function removeTreeNode() {
			hideRMenu();
			var nodes = zTree.getSelectedNodes();
			if (nodes && nodes.length>0) {
				if (nodes[0].children && nodes[0].children.length > 0) {
					alert("此机构下面有子机构，请先删除子机构");
					return false;
				} else {
					var treeNode = nodes[0];
					if(confirm("确认删除" + treeNode.name + "吗？")){
						var field = {organizationId:treeNode.id};
						$.ajax({
			      			type:"POST", 
			      			url:"/org/delete.htm",
			      			data:field ,
			      			async:false,
			      			success:function(result) {
					      	console.log(result);
					      		if(result.code=='1'){
					      			zTree.removeNode(nodes[0]);
					      		}else{
					      		 	alert(result.msg);
					      		}
					    	 	} 
					    });
						
					}
				}
			}
			
		}
		
		
	//修改名称
	function editTreeNode() {
		hideRMenu();
		var treeNode = zTree.getSelectedNodes()[0];
		zTree.editName(treeNode);
	    return true;
	}
	
	//修改名称
	function beforeRename(treeId, treeNode, newName, isCancel) {
		if (newName.length == 0) {
			setTimeout(function() {
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				zTree.cancelEditName();
				alert("节点名称不能为空.");
			}, 0);
			return false;
		}
		
		var field = {organizationId:treeNode.id,name:newName};
		
		var isRename = false;
		
		$.ajax({
  			type:"POST", 
  			url:"/org/modify.htm",
  			data:field ,
  			async:false,
  			success:function(result) {
	      
	      		if(result.code=='1'){
	      			isRename =  true;
	      			
	      		}else{
	      		 	alert(result.msg);
	      		}
	    	 	} 
	    }); 
	    
	    return isRename;
	}
	//移动
	function beforeDrop(treeId, treeNodes, targetNode, moveType) {
	
		treeNode = treeNodes[0];
		
		var field = {organizationId:treeNode.id};
		
		if(moveType =='inner'){
			field.parentId = targetNode.id
		}else{
			field.parentId = targetNode.pId
		}
		
		
		$.ajax({
  			type:"POST", 
  			url:"/org/move.htm",
  			data:field ,
  			async:false,
  			success:function(result) {
	      	console.log(result);
	      		if(result.code=='1'){
	      			isDelete =  true;
	      		}else{
	      		 	alert(result.msg);
	      		}
	    	 	} 
		});
		
	
		return targetNode ? targetNode.drop !== false : true;
	}
					
	function zTreeOnClick(event, treeId, treeNode) {
		var id = treeNode.id;
		//if(id !=0){
		//	if(treeNode.isParent){
		//		return false;
		//	}
		//}
		if(id == 0){
			parent.right.window.location.href = '/staff/list.htm'; 
		}else{
			parent.right.window.location.href = '/staff/list.htm?organizationId='+id; 
		}
	};		
		//-->
	</SCRIPT>	
	
	<style type="text/css">
		.rMenu {position:absolute; visibility:hidden; top:0; background-color: #555;text-align: left;padding: 2px;}
		
		.rMenu ul {
			margin: 0;
			padding: 0;
			border: 0;
			outline: 0;
			font-weight: inherit;
			font-style: inherit;
			font-size: 100%;
			font-family: inherit;
			vertical-align: baseline;
		}
		
		.rMenu ul li{
			margin: 1px 0;
			padding: 0 5px;
			cursor: pointer;
			list-style: none outside none;
			background-color: #DFDFDF;
		}
	</style>
</HEAD>

<BODY>
	<div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
	<div class="rMenu" id="rMenu">
		<ul>
			<li id="m_add" onclick="addTreeNode();">增加</li>
			<li id="m_del" onclick="removeTreeNode();">删除</li>
			<li id="m_edit" onclick="editTreeNode();">修改</li>
		</ul>
	</div>

</BODY>
</HTML>